<?php
/* 
 * Project: oCoder Framework
 * File: /models/subject.php
 * Purpose: model for the subject controller.
 * Author: Trung Hoang
 */

class SubjectModel extends BaseModel
{
    //data passed to the Subject index view
    public function index()
    {   
    	// get all Subject
    	$Subject = $this->find_all($table = 'subject AS s LEFT JOIN courses AS c ON s.courses_id = c.courses_id', $orderby = 'subject_id DESC', $where = '', $cols = ' s.subject_id, s.subject_name, c.courses_name, s.status, s.keyword, s.description ', $limit = '');
        $this->viewModel->set("pageTitle","Subject Manager");
        $this->viewModel->set("Subject",$Subject);
        
        $courses = $this->find_all($table = 'courses ',$orderby = 'courses_id DESC',  $where = '', $cols = ' courses_id, courses_name ', $limit = '');
        $this->viewModel->set("pageTitle","Subject Manager");
        $this->viewModel->set("courses", $courses);
        return $this->viewModel;
    }

    //create model
    public function create($data)
    {   
        //create new item when have data
    	if(!empty($data)){
            //return subject_id of item after insert to db
    		return $this->save($table = 'subject', $data = $data, $where = '');
    	}

        //param var for view
        $courses = $this->find_all("courses", "courses_id DESC", "");
        $this->viewModel->set("courses",$courses);
        $this->viewModel->set("pageTitle","Create Subject");
        return $this->viewModel;
    }

    //update model
    public function edit($subject_id, $data)
    {
        //update item when have data
        if(!empty($data)){
            //return bool
            return $this->save($table = 'subject', $data = $data, $where = 'subject_id='.$subject_id);
        }

        //get item detail by subject_id
        $subject = $this->find($table='subject', $orderby = 'subject_id DESC', $where = 'subject_id='.$subject_id, $cols = '*', $limit = '');
        $courses = $this->find_all("courses", "courses_id DESC", "", "*");
        
        //param var for view
        $this->viewModel->set("pageTitle","Update Subject");
        $this->viewModel->set("subject",$subject);
        $this->viewModel->set("courses",$courses);
        return $this->viewModel;
    }
    public function getList($class_id){
        $sbjs = $this->find_all($table = "classes AS cl INNER JOIN courses AS co ON cl.courses_id = co.courses_id LEFT JOIN subject AS sb ON co.courses_id = sb.courses_id", 
                                $orderby = "sb.subject_id ASC", 
                                $where = "classes_id = $class_id" , 
                                $cols = "sb.subject_id, sb.subject_name");
        $this->viewModel->set("sbjList",$sbjs);
        return $this->viewModel;
    }
	
	public function selectMulti($query){
		$this->query($query);
		$data = $this->get();
		$data = (empty($data)) ? array() : $data;
		$this->viewModel->set("data",$data);
		return $this->viewModel;
	}
}

?>